<?php
//用户登录检测
include_once('admin_global.php');
$db->Get_user_shell_check($uid, $shell);//检测用户是否登录

//显示树形栏目分类
$query=$db->select("category","*","");
//将查询结果存储到数组中
while($row=$db->fetch_array($query)){
    $data[]=$row;
}

$newsID = $_GET['newsID'];       //获得文章ID
//根据文章ID查询其所在栏目ID
$query=$db->select("news","*","newsID=$newsID");
while ($row = $db->fetch_array()) {
    $rs=$row;  //存储文章内容
    $cateID = $row['newsCID'];   //取得文章所在栏目ID
}
$tree = new Tree($data);       //树形类实例化
$gettree = $tree->get_tree(0, "<option value=\$id \$selected>\$spacer\$cateName</option>", $cateID);  //得到树形结构

//如果是单页栏目的文章
$query=$db->select("category","*","id=$cateID");
while ($row = $db->fetch_array()) {
    $cateStyleID=$row['cateStyleID'];
}
if($cateStyleID==2){
    echo "<script>alert('不能在此编辑单页栏目文章！');history.go(-1);</script>";
    exit();
}
//修改文章
if(isset($_POST['editNews'])){
    //不能选择含有子栏目的栏目
    $cateID = $_POST['pid'];
    $query=$db->select('category',"*","catePID=$cateID");
    $nums=$db->db_num_rows();
    if($nums>0){
        echo "<script>alert('不能选择含有子栏目的栏目！');history.go(-1);</script>";
    }else{
        //不能选择单页栏目
        $query=$db->select('category',"*","id=$cateID");
        $result=$db->fetch_array();
        $cateStyleID=$result['cateStyleID'];
        if($cateStyleID==2){
            echo "<script>alert('不能选择单页栏目！');history.go(-1);</script>";
        }else{
            //获取POST数据，准备写入数据库
            $newsCID=$_POST['pid'];
            $newsTitle=trim($_POST['newsTitle']);
            $newsKeyWords = $_POST['newsKeyWords'];
            $newsAuthor = $_POST['newsAuthor'];
            $newsSource = $_POST['newsSource'];
            if (!empty($_POST['isTop'])) {
                $isTop = 1;
            } else {
                $isTop = 0;
            }
            if (!empty($_POST['isRed'])) {
                $isRed = 1;
            } else {
                $isRed = 0;
            }
            $newsUpdateTime=time();
            //判断是否有图片
            $newsContentTMP = $_POST['content'];
            $newsContentJJ=  mb_substr(strip_tags($newsContentTMP),0,90,'utf-8');
            $patten = '/<img.+src=\"?(.+\.(jpg|jpeg|bmp|bnp|png))\"?.+>/i';
            if (preg_match($patten, $newsContentTMP, $matche)) {
                $newsImagePath = $matche[1];
            } else {
                $newsImagePath = "";
            }
            if (!get_magic_quotes_gpc()) {
                $newsContent = addslashes($_POST['content']);
            } else {
                $newsContent = $_POST['content'];
            }
            //写入数据库
            $rows=$db->update("news","newsCID=$newsCID,newsAuthor='$newsAuthor',newsTitle='$newsTitle',newsSource='$newsSource',isTop=$isTop,isRed=$isRed,newsUpdateTime=$newsUpdateTime,newsImagePath='$newsImagePath',newsKeyWords='$newsKeyWords',newsContent='$newsContent',newsContentJJ='$newsContentJJ'","newsID=$newsID");
            if($rows>0){
                if($_SESSION['userType']=='普通管理员'){
                    echo "<script>alert('文章修改成功！');window.location.href='listMyNews.php'</script>";
                }else{
                    echo "<script>alert('文章修改成功！');window.location.href='listNews.php'</script>";
                }
            }else{
                echo "<script>alert('文章修改失败！');history.go(-1);</script>";
            }
        }
    }
}

?>?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>编辑文章</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" /> 
        <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
        <script type="text/javascript" src="js/jquery.validate-1.13.1.js"></script>
        <!--UE编辑器-->
        <!-- 配置文件 -->
        <script type="text/javascript" src="../ueditor/ueditor.config.js"></script>
        <!-- 编辑器源码文件 -->
        <script type="text/javascript" src="../ueditor/ueditor.all.js"></script>
        <!-- 实例化编辑器 -->
        <script type="text/javascript">
            $(function(){
                var ue = UE.getEditor('container',{
                    autoHeightEnabled: true
                });
            })
        </script>
    </head>
    <script>
        $(function(){
            $("#form1").validate({
                rules:{
                    newsTitle:{
                        required:true,
                        rangelength:[6,30]
                    },
                    newsAuthor:{
                        required:true
                    }
                },
                messages:{
                    newsTitle:{
                        required:"请输入文章标题！",
                        rangelength:"文章标题不得少于6个字符！"
                    },
                    newsAuthor:{
                        required:"请输入文章作者！"
                    }
                }
            })
        })
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 文章管理 > 添编辑文章
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >编辑文章</th>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">所属栏目：</td>
                                            <td width="90%">
                                                <select name="pid">
                                                    <?php print_r($gettree); ?> 
                                                </select> *不能指定为含有子栏目的栏目！
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章标题：</td>
                                            <td width="90%"><input type="text" name="newsTitle"  class="int_text" value="<?php echo $rs['newsTitle']; ?>" size="60"  maxlength="60"/> *文章标题不能少于6个字符！</td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">关键字：</td>
                                            <td width="90%"><input type="text" name="newsKeyWords"  class="int_text" value="<?php echo $rs['newsKeyWords']; ?>" size="40"  maxlength="40"/></td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章作者：</td>
                                            <td width="90%"><input type="text" name="newsAuthor"  class="int_text" value="<?php echo $rs['newsAuthor']; ?>" size="40"  maxlength="40"/> *必填</td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章来源：</td>
                                            <td width="90%"><input type="text" name="newsSource"  class="int_text" value="<?php echo $rs['newsSource']; ?>" size="40"  maxlength="40"/></td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">是否置顶：</td>
                                            <td width="90%"><input type="checkbox" name="isTop"  class="int_text" value="6" size="40" <?php if ($rs['isTop'] == 1) {  echo "checked='checked'";  } ?> /></td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">是否加红：</td>
                                            <td width="90%"><input type="checkbox" name="isRed"  class="int_text" value="6" size="40" <?php if ($rs['isRed'] == 1) {  echo "checked='checked'";  } ?> /></td>
                                        </tr>   
                                        <tr>
                                            <td width="10%" align="right">文章内容：</td>
                                            <td width="90%">
                                                <script id="container" name="content" style="height:300px" type="text/plain"><?php echo $rs['newsContent']; ?></script>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" name="editNews" class="btn_ipt01" value=" 确认修改 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
